package com.yocard.yxyd.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.yocard.yxyd.R;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.http.model.base.Response;
import com.yocard.yxyd.http.model.response.CertStatusBean;
import com.yocard.yxyd.http.model.response.UserInfoBean;
import com.yocard.yxyd.ui.activity.base.BaseActivity;
import com.yocard.yxyd.ui.contract.CertResultContract;
import com.yocard.yxyd.ui.presenter.CertResultPresenter;
import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.utils.util.SPUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created on 2019/6/4.
 * 认证结果
 */
public class CertResultActivity extends BaseActivity<CertResultPresenter> implements CertResultContract.View {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_ico)
    ImageView ivIco;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.btn_next)
    Button btnNext;

    protected String approval_status;
    @BindView(R.id.tv_status)
    TextView tvStatus;

    @Override
    protected int getLayout() {
        return R.layout.act_cert_result;
    }

    @Override
    protected void initEventAndData() {
        initTitle();
        showLoadingDialog();
        mPresenter.fetchUserData();
//        initView();
    }

    private void initView() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            approval_status = extras.getString("approval_status");
        }

        if (!Constants.AUTH_APPROVING.equals(approval_status)) {
            ivIco.setImageResource(R.mipmap.img_renzheng_fail);
            btnNext.setVisibility(View.VISIBLE);
            tvStatus.setText(R.string.cert_failed);
            tvTips.setTextColor(getResources().getColor(R.color.color_ff001f));
            showLoadingDialog();
            getFailDetail();
        }
    }

    @SuppressLint("CheckResult")
    private void getFailDetail() {
        HttpManager.getInstance().getCertStatus()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Response<CertStatusBean>>() {
                    @Override
                    public void accept(Response<CertStatusBean> beanResponse) throws Exception {
                        try {
                            tvTips.setText("原因：" + beanResponse.getData().getApproval().get(0).getDetail());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void initTitle() {
        ImmersionBar.with(this).statusBarColor(R.color.white)
                .statusBarDarkFont(true).navigationBarColor(R.color.white).init();
        tvTitle.setText("信贷经理认证");
    }

    @OnClick({R.id.iv_back, R.id.btn_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ActivityUtils.finishActivity(this);
                break;
            case R.id.btn_next:
                Bundle bundle = new Bundle();
                bundle.putBoolean("isReCert", true);
                ActivityUtils.startActivity(bundle, CertificationActivity.class);
                ActivityUtils.finishActivity(this);
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onFetchUser(UserInfoBean userInfoBean) {
        dismisDialog();
        if (userInfoBean != null)
            SPUtils.getInstance().put(Constants.USER_PHONE, userInfoBean.getId());
        approval_status = userInfoBean.getApproval_status();
        if (Constants.AUTH_FAILURE.equals(approval_status)) {
            ivIco.setImageResource(R.mipmap.img_renzheng_fail);
            btnNext.setVisibility(View.VISIBLE);
            tvStatus.setText(R.string.cert_failed);
            tvTips.setTextColor(getResources().getColor(R.color.color_ff001f));
            getFailDetail();
            return;
        }
        ivIco.setImageResource(R.mipmap.img_renzheng_ing);
        btnNext.setVisibility(View.GONE);
        tvStatus.setText(R.string.cert_ing);
        tvTips.setText(R.string.cert_ing_tip);
    }
}
